package com.task.core.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.PutObjectResult;
import com.task.common.constant.AliyunConstants;
import com.task.common.exception.ServiceException;
import com.task.common.utils.uuid.UUID;
import com.task.core.domain.SysFileInfo;
import com.task.core.service.AliyunOssService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class AliyunOssServiceImpl implements AliyunOssService {

    private final static DateTimeFormatter yFormatter = DateTimeFormatter.ofPattern("yyyy");
    private final static DateTimeFormatter mFormatter = DateTimeFormatter.ofPattern("MM");
    private final static DateTimeFormatter dFormatter = DateTimeFormatter.ofPattern("dd");

    private final static String accessUrl = "https://"+ AliyunConstants.BUCKET_NAME+".oss-cn-hangzhou.aliyuncs.com/";

    @Autowired
    private OSS ossClient;

    /** 上传文件  **/
    public SysFileInfo uploadFile(MultipartFile file)  {

        if(ObjectUtils.isEmpty(file)){
            throw new  ServiceException("文件不能为空");
        }

        String fileUrl = String.valueOf("");
        String originName = file.getOriginalFilename();
        String endName = null;//提取文件名后缀,仅处理简单后缀名
        if (originName.lastIndexOf(".") >= 0) {
            endName = originName.substring(originName.lastIndexOf("."));
        }

        //文件名
        String newName = UUID.randomUUID().toString()+endName;
        //文件夹
        String folderName = yFormatter.format(LocalDateTime.now())+"/"
                + mFormatter.format(LocalDateTime.now())+ "/"
                +dFormatter.format(LocalDateTime.now())+"/";

        try {
            //含文件夹名的文件名
            String fileName = folderName+newName;
            InputStream inputStream = file.getInputStream();

            fileUrl = this.uploadFileStream(inputStream, fileName);
        } catch (IOException e) {
            throw new ServiceException("文件流异常");
        }
        SysFileInfo sysFileInfo = new SysFileInfo();
        sysFileInfo.setUrl(fileUrl);
        return sysFileInfo;
    }


    /**
     * 底层上传文件至阿里云服务器
     * @param inputStream 传输的文件流
     * @param fileName 自定义文件名（含文件夹）
     * @return 上传后的文件路径
     * @throws Exception
     */
    private String uploadFileStream(InputStream inputStream,String fileName)  {

        String fileUrl = String.valueOf("");
        try {
            PutObjectResult putObjectResult = ossClient.putObject(AliyunConstants.BUCKET_NAME, fileName, inputStream);
            fileUrl = accessUrl+fileName;
        }catch (Exception e) {
            throw new ServiceException("上传文件异常");
        }
        return fileUrl;
    }

}
